package org.zsqx.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.Properties;

import cDao.HelpDao;

public class PropertiesTools {
	private static final String propertiesFileName = "qxjSet.properties";
	public static String domain;
	private Properties properties;
	private File file;
	public PropertiesTools(){
		String fileName= System.getProperty("user.dir")+File.separator+propertiesFileName;
		properties= new Properties();
		file = new File(fileName);
	}
	public String getPropertiesValue(String key){
		String value = "0";
		if(file.exists()){
			try {
				properties.load(new FileInputStream(file));
			} catch (FileNotFoundException e) {
				System.out.println("文件不存在");
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(properties.get(key)!=null)
				value = properties.get(key).toString();
		}
		return value;
	}
	public Object getPropertyesBean(Object bean){
		if(file.exists()){
			try {
				properties.load(new FileInputStream(file));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			Class c = bean.getClass();
			Field[] fields = c.getDeclaredFields();
			for (Field field : fields) {
				String fieldName = field.getName();
				String methodName = HelpDao.getSetMethod(fieldName);
				if(null!=properties.get(fieldName)){
					Object value = null;
					if(field.getType()==Double.class){
						value = Double.parseDouble(properties.get(fieldName).toString());
					}else{
						value = properties.get(fieldName);
					}
					HelpDao.methodInvoke(c, bean, methodName, field.getType(), value);
				}
			}
		}
		return bean;
	}
	public void setPropertiesValue(String key,String value){
		try {
			if(file.exists()){
				properties.load(new FileInputStream(file));
			}
			properties.setProperty(key, value);
			properties.store(new FileOutputStream(file), "change value"+key);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void setPropertiesBean(Object bean){
		Class c = bean.getClass();
		Field[] fields = c.getDeclaredFields();
		try {
			if(file.exists()){
				properties.load(new FileInputStream(file));
			}
			for (Field field : fields) {
				String fieldName = field.getName();
				String methodName = HelpDao.getGetMethod(fieldName);
				Object fieldValue = HelpDao.methodInvoke(c, bean, methodName);
				if(null!=fieldValue){
					properties.setProperty(fieldName, fieldValue.toString());
				}
			}
			properties.store(new FileOutputStream(file), "change value is change "+new Date());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}
